package com.upload.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.whatisjava.action.BaseAction;

public class FileUploadAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    // 该名要与表单的名字对应
    private File photo;

    // 必须按照这个格式 [表单的名字＋FileName]_[photoFileName]
    private String photoFileName;

    public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }

    public String getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }

    /**
     * 显示页面
     * 
     * @return
     */
    public String showForm() {
        return "success";
    }

    /**
     * @return
     */
    public String execute() {
        String path = "upload";
        path = application.getRealPath(path);
        File imageFile = new File(path, photoFileName);
        photo.renameTo(imageFile);

        try {
            BufferedImage buff = ImageIO.read(imageFile);
            Integer width = buff.getWidth();
            Integer height = buff.getHeight();
            if (width > height) {
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }
}
